local skel = fk.CreateSkill {
  name = "emo__huahu",
}

Fk:loadTranslationTable{
  ["emo__huahu"] = "画虎",
  [":emo__huahu"] = "每回合结束时，你可以将X张牌当本回合进入弃牌堆的一张普通锦囊牌使用（X为你以此法使用同名牌的次数）。",

  ["#emo__huahu-use"] = "画虎：你可以转化使用一张本回合进入弃牌堆的普通锦囊牌",
  ["#emo__huahu-viewas"] = "画虎：将 %arg 张牌转化为【%arg2】使用",
  ["emo__huahu_viewas"] = "画虎",
}

skel:addEffect(fk.TurnEnd, {
  can_trigger = function (self, event, target, player, data)
    if not player:hasSkill(skel.name) or player:isNude() then return false end
    local cards = {}
    player.room.logic:getEventsOfScope(GameEvent.MoveCards, 1, function(e)
      for _, move in ipairs(e.data) do
        if move.toArea == Card.DiscardPile then
          for _, info in ipairs(move.moveInfo) do
            local card = Fk:getCardById(info.cardId, true)
            if card:isCommonTrick() then
              table.insertIfNeed(cards, card.name)
            end
          end
        end
      end
      return false
    end, Player.HistoryTurn)
    cards = table.filter(cards, function (name)
      local c = Fk:cloneCard(name)
      c.skillName = skel.name
      return player:canUse(c)
    end)
    if #cards > 0 then
      event:setCostData(self, cards)
      return true
    end
  end,
  on_cost = function (self, event, target, player, data)
    local names = event:getCostData(self)
    local _, dat = player.room:askToUseActiveSkill(player, {
      skill_name = "emo__huahu_viewas", prompt = "#emo__huahu-use", skip = true,
      extra_data = {emo__huahu_names = names}
    })
    if dat then
      event:setCostData(self, { cards = dat.cards, tos = dat.targets, name = dat.interaction })
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local cost_data = event:getCostData(self)
    local card = Fk:cloneCard(cost_data.name)
    card:addSubcards(cost_data.cards)
    card.skillName = skel.name
    local tag = player:getTableMark("emo__huahu_record")
    tag[card.name] = (tag[card.name] or 1) + 1
    room:setPlayerMark(player, "emo__huahu_record", tag)
    room:useCard{
      from = player,
      tos = cost_data.tos,
      card = card,
      extraUse = true,
    }
  end,
})

local skel2 = fk.CreateSkill {
  name = "emo__huahu_viewas",
}

skel2:addEffect("viewas", {
  prompt = function (self, player, selected_cards, selected)
    local name = self.interaction.data
    if name ~= nil then
      local tag = player:getTableMark("emo__huahu_record")
      local n = tag[name] or 1
      return "#emo__huahu-viewas:::" .. n .. ":" .. name
    end
    return "#emo__huahu-use"
  end,
  interaction = function (self, player)
    return UI.CardNameBox{ choices = self.emo__huahu_names or {} }
  end,
  card_filter = function (self, player, to_select, selected)
    local name = self.interaction.data
    if name ~= nil then
      local tag = player:getTableMark("emo__huahu_record")
      local n = tag[name] or 1
      return #selected < n
    end
  end,
  view_as = function (self, player, cards)
    local name = self.interaction.data
    if name ~= nil then
      local tag = player:getTableMark("emo__huahu_record")
      local n = tag[name] or 1
      if #cards == n then
        local c = Fk:cloneCard(name)
        c.skillName = skel.name
        c:addSubcards(cards)
        return c
      end
    end
  end,
})

return {skel, skel2}
